package ece452.ims.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import ece452.ims.connection.Relay;
import java.util.*;

public class BuddyList extends JPanel{
	
	private JList blist;
	private ArrayList<ChatWindow> chats;
	private DefaultListModel listOfFriends;
	private static Relay rl;
	//private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();	
	
	public BuddyList(Relay r, DefaultListModel fl){
		super(new GridLayout(1,1));
		this.rl = r;
		this.listOfFriends = fl;
		chats = new ArrayList<ChatWindow>();
		blist = new JList(listOfFriends);
		JScrollPane scroller = new JScrollPane(blist);
		blist.addMouseListener(new MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				if (evt.getClickCount() == 2) {
					int index = blist.getSelectedIndex();
					String whole = listOfFriends.getElementAt(index).toString();
					String name = whole.substring(0, whole.indexOf(" "));
					for(int i=0; i<chats.size(); ++i){
						//if(chats.get(i).getName().toString().equals(name)) return;
					}
					ChatWindow cw = new ChatWindow(rl,name);
					chats.add(cw);
					newChatWindow(cw, name);
				}
			}
		});
		add(scroller);
	}
	
	public void updateList(String n){
		String n2 = n.substring(n.indexOf("`")+1, n.lastIndexOf("`"));
		String s2 = n.substring(n.lastIndexOf("`")+1);
		for(int i=0; i<listOfFriends.size(); ++i){
			String whole = listOfFriends.getElementAt(i).toString();
			String n1 = whole.substring(0, whole.indexOf(" "));
			if(n1.equals(n2)) listOfFriends.setElementAt(n2+" - "+s2, i);
		}
	}
	
	public void updateStatus(String str, int n){
		String name = str.substring(str.indexOf("`")+1);
		for(int i=0; i<listOfFriends.size(); ++i){
			String wh = listOfFriends.get(i).toString();
			String lname = wh.substring(0, wh.indexOf(" "));
			String status = wh.substring(wh.lastIndexOf(" ")+1);
			switch(n){
				case 15:	// squelch
					if(lname.equals(name)){
						wh = wh.replaceAll(status, "squelched");
						listOfFriends.set(i, wh);
						break;
					}
				case 16:	// unsquelch
					if(lname.equals(name)){
						wh = wh.replaceAll(status, "online");
						listOfFriends.set(i,wh);
						break;
					}
				default:
					break;
			}
		}
	}
	
	public void addFriend(String f){
			for(int i=0; i<listOfFriends.size(); ++i){
				String who = listOfFriends.getElementAt(i).toString();
				String un = who.substring(0, who.indexOf(" "));
				if(un.equals(f)) return;
			}
			listOfFriends.addElement(f+" - offline");
			rl.getListOfFriends();
	}
	
	public void removeFriend(String f){
		for(int i=0; i<listOfFriends.size(); ++i){
			String who = listOfFriends.getElementAt(i).toString();
			String un = who.substring(0, who.indexOf(" "));
			if(f.equals(un)){
				listOfFriends.remove(i);
				return;
			}
		}
		rl.getListOfFriends();
	}
	
	// update chat window
	public void updateChat(String u, String m){
		for(int i=0; i<chats.size(); ++i){
			if(chats.get(i).getName().toString().equals(u)){
				if(!chats.get(i).isVisible()){
					chats.get(i).setVisible(true);
				}
				chats.get(i).updateReceive(u+": "+m);
				return;
			}if(!chats.get(i).isVisible()){
				chats.remove(i);
			}
		}
		//	}else{
		ChatWindow cw = new ChatWindow(rl,u);
		chats.add(cw);
		newChatWindow(cw,u);
		updateChat(u,m);
	}
	
	public JMenuBar getBLMenuBar(){
		JMenuBar bar = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenuItem addFriend = new JMenuItem("Add Friend");
		addFriend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				AddRemoveFriend ar = new AddRemoveFriend(rl,1);
				newFriendWindow(ar,1);
			}
		});
		JMenuItem removeFriend = new JMenuItem("Remove Friend");
		removeFriend.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				AddRemoveFriend ar = new AddRemoveFriend(rl,2);
				newFriendWindow(ar,2);
				removeFriend(ar.remF);
			}
		});
		JMenuItem logout = new JMenuItem("Exit");
		logout.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				rl.logout();
				//Login.bl.setVisible(false);
				//Login.mainFrame.setVisible(true);
			}
		});
		file.add(addFriend);
		file.add(removeFriend);
		file.addSeparator();
		file.add(logout);
		bar.add(file);
		return bar;
	}
	
	protected JComponent makeTextPanel(String text) {
		JPanel panel = new JPanel(true);
		JLabel filler = new JLabel(text);
		filler.setHorizontalAlignment(JLabel.CENTER);
		panel.setLayout(new GridLayout(1, 1));
		panel.add(filler);
		return panel;
	}
	
	public void newChatWindow(ChatWindow cw, String name){
		JFrame frame = new JFrame(name);
		frame.setName(name);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setPreferredSize(new Dimension(310, 302));
		frame.getContentPane().add(cw, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setJMenuBar(cw.getCWMenuBar()); // should this be static?
		frame.setResizable(false);
		frame.setLocation(Login.screenSize.height/2, Login.screenSize.width/6);
	}
	
	public void newFriendWindow(AddRemoveFriend ar, int n){
		String label="";
		if(n==1) label = "Add Friend";
		else label = "Remove Friend";
		JFrame frame = new JFrame(label);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setPreferredSize(new Dimension(250, 200));
		frame.getContentPane().add(ar, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setLocation(Login.screenSize.height/2, Login.screenSize.width/6);
	}
	
	/* For testing purposes only */
	public static void showGUI() {
		BuddyList bl = new BuddyList(null,null);
		JFrame frame = new JFrame("Friends List");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// frame.setPreferredSize(new Dimension(320,280));
		frame.setPreferredSize(new Dimension(250, 520));
		frame.getContentPane().add(bl, BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
		frame.setJMenuBar(bl.getBLMenuBar());
	}
	
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() { showGUI(); }
		});
	}
}
